from xml.etree import ElementTree

def Test_Print(char_string):
    if 1: #用于开关打印
        print(char_string)

Tab = ""
n = 0
Get_Postion = False
def Root_Traverse(root):
    global Tab
    global n
    global Get_Postion
    Last_root = "AUTOSAR"
    for child in root:
        if Last_root != root:
            n += 1
            Tab += "    "
            Last_root = root
        test_print_log = f"{Tab}{child.tag},{child.attrib},{child.text}  ,:{n}"
        Test_Print(test_print_log)
        if child.tag == "SHORT-NAME" and child.text == "axvRefMs2":
            Get_Postion = True
        if True == Get_Postion and child.tag == 'SW-DATA-DEF-PROPS-VARIANTS':
            Get_Postion = False
            Currnt_Child = ElementTree.SubElement(root,'test_child')
            Currnt_Child.text = 'lalalademaxiya'

        #嵌套调用，遍历所有树
        Root_Traverse(child)


tree = ElementTree.parse('autosar_swc_vehinfo_interface.arxml') # 直接读取xml文件，形成ElementTree结构
root = tree.getroot() # 获取root tag



Root_Traverse(root)
#五角星中的五角星 格式化打印
ElementTree.indent(root)
File_Output = ElementTree.tostring(root,encoding='unicode')

with open('output11.xml', 'w', encoding='utf-8') as file:
    file.write(File_Output) 